package com.leyou.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.pojo.Item;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class ItemService {
    public Item saveItem(Item item){
        //判断价格是否为空
        if(item.getPrice()==null){
            throw new LyException(ExceptionEnum.PRICE_CANT_NULL);
        }
        if(item.getName()==null||item.getName().trim().equals("")){
            throw new LyException(ExceptionEnum.NAME_CANT_NULL);
        }
        //随机生成id，模拟数据库的新增
        int id=new Random().nextInt(100);
        item.setId(id);
        //返回新增之后的对象，回显id
        return item;
    }
}
